﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Coolite.Ext.Web;
using GUESTSMANAGEMENT.Controller;

namespace GUESTSMANAGEMENT.View.Guets
{
    public partial class grdGuests : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Carrega os dados na grid quando abre a tela.
            if (!Ext.IsAjaxRequest)
            {
                this.guestsStore.DataSource = Controller.Guests.getGridList();
                this.guestsStore.DataBind();
            }
        }

        //Reload na grid quando salva ou atualiza os dados.
        public Store GridStore
        {
            get;
            set;
        }

        //Chama método GridStore
        protected void Page_Init(object sender, EventArgs e)
        {
            this.GMGuests.GridStore = this.guestsStore;
            this.GridStore = this.guestsStore;
        }

        //Atualiza grid quando á dado refesh.
        protected void Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            this.guestsStore.DataSource = Controller.Guests.getGridList();
            this.guestsStore.DataBind();
        }

        //Edita o pedido cadastrado.
        protected void EditGuests(object sender, AjaxEventArgs e)
        {
            string id = e.ExtraParams["id"];
            this.GMGuests.SetGuests(Controller.Guests.RetornarGuests(int.Parse(id)));
            this.GMGuests.ShowGuests();
        }

        //Método para excluir.
        protected void ConfirmDeleteGuests(object sender, AjaxEventArgs e)
        {
            string selectGuests = this.txt_idGuests.Text.Trim();
            gmguests guests = Controller.Guests.getByGuests(int.Parse(selectGuests));
            Controller.Guests.deletGuests(guests);
            this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
            Ext.Msg.Alert("Alerta!", "Convidado <b>" + guests.NMGUESTS + "</b> excluído com sucesso!").Show();

        }
    }
}